package com.hc.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.bean.SysUserBean;
import com.hc.bean.SysUserRoleBean;
import com.hc.mapper.SysUserMapper;
import com.hc.mapper.SysUserRoleMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public PageInfo<SysUserBean> queryDataByPage(SysUserBean sysUserBean, int pageNum, int pageSize) {

         PageHelper.startPage(pageNum,pageSize);//设置分页参数
         List<SysUserBean> sysUserBeanList =   sysUserMapper.queryData(sysUserBean);
         PageInfo<SysUserBean> pageInfo = new PageInfo<>(sysUserBeanList); //把查询结果放到PageInfo对象中

        return pageInfo;
    }

    @Override
    @Transactional   //JDBC
    public int insertData(SysUserBean sysUserBean) {
        //1.判断用户名是否已存在
           SysUserBean syb =   sysUserMapper.queryDataByUserName(sysUserBean.getUsername());
           if (syb!=null){  //代表用户已存在
               throw  new RuntimeException("该用户名已存在");
           }

           //方法参数sysUserBean中的密码是明文的，需要加密后，再存储到数据库
         String encPwd = BCrypt.hashpw(sysUserBean.getPassword(),BCrypt.gensalt());

           sysUserBean.setPassword(encPwd);
        //2 插入数据

        //往管理员表tz_sys_user表中写数据，该方法的返回值只是表示执行结果(影响的行)
        //如果要拿到insert语句执行成功后的主键列值，需要在配置文件中添加useGeneratedKeys="true" keyProperty="user_id"

        System.out.println("insert前的的userId:"+sysUserBean.getUser_id());
        int res =  sysUserMapper.insertData(sysUserBean);
        System.out.println("insert后的的userId:"+sysUserBean.getUser_id());

        System.out.println("前端传递过来的角色ID为:"+sysUserBean.getSelectedRoleId());
        //先往管理员表中写入管理的信息
        //再根据SelectedRoleId往tz_sys_user_role中间表中写数据


        //AOP
        for(Integer roleId:sysUserBean.getSelectedRoleId()){
            //调用sysUserRoleMapper中的insertData方法
            SysUserRoleBean srb = new SysUserRoleBean();
            srb.setUser_id(sysUserBean.getUser_id());
            srb.setRole_id(roleId);
            sysUserRoleMapper.insertData(srb);
        }



        return  res;
    }

    @Override
    public int deleteDataById(Integer user_id) {

        return sysUserMapper.deleteDataById(user_id);
    }

    @Override
    public SysUserBean userLogin(String userName, String password) {
        //1.根据用户名获得SysUserBean对象
         SysUserBean syb =  sysUserMapper.queryDataByUserName(userName);
         if (syb==null){
             throw  new RuntimeException("用户名不存在");
         }


         if (syb.getStatus().equals(0)){
            throw  new RuntimeException("该用户已被禁用");
         }

        //2 取出 SysUserBean对象的password属性(数据库中那个加密的数据)
       boolean  flag =    BCrypt.checkpw(password,syb.getPassword());
         if (!flag){
             throw  new RuntimeException("用户名或密码错误");
         }

        //3调用 BCrypt.checkpw("123455",p)方法验证前端传递过明文密码与数据库中的是否一致
        //4验证通过返回SysUserBean对象，否则抛出异常
        return syb;
    }
}
